'use strict'

class notice {
    constructor() {
        this.param = '/'
    }
    //创建资源
    async post(c) {
        var content = JSON.parse(c.body.toString('utf-8')).notice_content;
        try {
            let sqltext1 = `SELECT notice_id FROM notice`
            var r1 = await c.service.psql.query(sqltext1);
            if (r1.rowCount == 0) {
                return c.send('have no user');
            }
            var num = r1.rows[0].notice_id;
            for(var i = 0; i <r1.rows.length;i++){
                var notice_id = r1.rows[i].notice_id;
                if(notice_id > num){
                    num = notice_id;
                }
            }
            var a = parseInt(num) + 1;
            let sqltext2 = `INSERT INTO notice (notice_id,notice_content) VALUES (${a} , '${content}')`
            var r2 = await c.service.psql.query(sqltext2);
            c.send("success");
            return
        }
        catch (err) {
            console.log(err);
            c.status(500).send('error');
        }
    }
}

module.exports = notice